<?php

namespace app\api\controller;

use app\api\model\Item as ItemModel;
use app\api\model\Cart as CartModel;
use app\common\service\qrcode\Item as ItemPoster;

/**
 * 商品控制器
 * Class Item
 * @package app\api\controller
 */
class Item extends Controller
{

    public function initialize()
    {
        $this->user = $this->getShangUser();   // 商户信息
        $this->imgadess = $this->pcUrl();
    }

    /**
     * 商品添加
     */
    public function edit()
    {
        $userId = $this->user['merchant_user_id'];
        $model = new ItemModel;
        $info = $this->request->post();
        // 商品图片
        if (!isset($info['img_good']) || empty($info['img_good'])) {
            return $this->renderError(1001,'请上传商品图片');
        }
        //商品规格
        if (!isset($info['good_guige']) || empty($info['good_guige'])) {
            return $this->renderError(1001,'请设置商品规格');
        }
        if (!isset($info['good_pinpai']) || empty($info['good_pinpai'])) {
            return $this->renderError(1001,'请添加商品品牌');
        }

        if ( empty($info['good_state'])||empty($info['img_array'])||empty($info['good_sum'])||empty($info['good_money'])||empty($info['good_kucun'])||empty($info['province_id'])||empty($info['city_id'])||empty($info['district_id'])) {
            return $this->renderError(1001,'缺少参数');
        }
        if(empty($info['good_num'])){
            $info['good_num'] = '8'.time().$userId;
        }
        $date = $model->addGood($info,$userId);
        if($date){
            return $this->renderSuccess(1000,'成功',$data = []);
        }
        return $this->renderError(1001,'失败');

    }


    /**
     * 商品列表
     */
    public function lists()
    {
        $info = $this->request->post();
        $model = new ItemModel;
        if(empty($info['type'])){
            $info['type'] = '';
        }
        $list = $model->getShangList($this->user['merchant_user_id'],$info['tates'],$info['type']);
        //查找图片
        if(count($list['data'])>0){
            foreach ($list['data'] as $key=>$vle){
                $list['data'][$key]['good_imagead'] = $this->imgadess.$vle['good_image'].'.jpg';
            }
        }
        return $this->renderSuccess(1000,'成功',compact('list'));
    }

    /**
     * 商品详情
     */
    public function detailad()
    {
        // 商品详情
        $info = $this->request->post();
        $data=[];
        $detail = ItemModel::detailad($info['id']);
        if (!$detail) {
            return $this->renderError(1001,'很抱歉，商品信息不存在或已下架');
        }
        if(!empty($detail['good_image'])){
            $detail['good_imagead'] = $this->imgadess.$detail['good_image'].'.jpg';
        }
        //查找图片
        $imgarr = ItemModel::detailimg($detail['goods_id']);
        if(count($imgarr)>0){
            foreach ($imgarr as $key=>$vle){
                $imgarr[$key]['imagead'] = $this->imgadess.$vle['image_id'].'.jpg';
            }
        }
        $detail['img_array'] = $imgarr;
        $model = new ItemModel();
        $province = $model->provinceList($detail['province_id']);
        $citylist = '';
        if(!empty($detail['city_id'])){
            $citylist = $model->cityList($detail['city_id']);
        }
        $dislist='';
        if(!empty($detail['district_id'])){
            $dislist = $model->districtList($detail['district_id']);
        }
        if(empty($province['name'])){
            $province['name'] = '';
        }
        if(empty($citylist['name'])){
            $citylist['name'] = '';
        }
        if(empty($dislist['name'])){
            $dislist['name'] = '';
        }
        $detail['address'] = $province['name'].$citylist['name'].$dislist['name'];
        return $this->renderSuccess(1000,'成功',compact('detail'));
    }

    /**
     * 商品上下架
     */
    public function updatestate()
    {
        // 商品详情
        $info = $this->request->post();
        $data=[];
        if(!empty($info['id'])&&!empty($info['state'])){
            $detail = ItemModel::updatestate($info['id'],$info['state']);
            if (!$detail) {
                return $this->renderError(1001,'失败');
            }
            return $this->renderSuccess(1000,'成功');
        }
        return $this->renderError(1002,'缺少参数');

    }

    /**
     * 商品删除
     */
    public function itemDelect()
    {
        $info = $this->request->post();
        if(empty($info['goods_id'])){
            return $this->renderError(1001,'缺少参数');
        }
        $where['goods_id'] = $info['goods_id'];
        $model = new ItemModel;
        $list = $model::itemDelect($where);
        if($list){
            return $this->renderSuccess('1000','成功');
        }
        return $this->renderSuccess('1002','失败');

    }

    /**
     * 商品添加
     */
    public function itemAdd()
    {
        $userId = $this->user['merchant_user_id'];
        $model = new ItemModel;
        $info = $this->request->post();
        // 商品图片
        if (!isset($info['img_good']) || empty($info['img_good'])) {
            return $this->renderError(1001,'请上传商品图片');
        }
        //商品规格
        if (!isset($info['good_guige']) || empty($info['good_guige'])) {
            return $this->renderError(1001,'请设置商品规格');
        }
        if (!isset($info['good_pinpai']) || empty($info['good_pinpai'])) {
            return $this->renderError(1001,'请添加商品品牌');
        }

        if ( empty($info['good_state'])||empty($info['img_array'])||empty($info['good_sum'])||empty($info['good_money'])||empty($info['good_kucun'])||empty($info['province_id'])||empty($info['city_id'])||empty($info['district_id'])) {
            return $this->renderError(1001,'缺少参数');
        }
        if(empty($info['good_num'])){
            $info['good_num'] = '8'.time().$userId;
        }
        $date = $model->itemAdd($info,$userId);
        if($date){
            return $this->renderSuccess(1000,'成功',$data = []);
        }
        return $this->renderError(1001,'失败');

    }

}
